package cn.Quartz.Scheduler;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TaskServiceImpl  {

//    @Autowired
//    private TaskDao taskDao;

/*    public void initJob() {
        System.out.println("测试测试。。。。");
        System.out.println("Quartz 初始化 ***********************************************");
        log.debug("Quartz Scheduler HashCode ："+String.valueOf(SchedulerInstance.getInstance().hashCode()));
        List<Task> taskList = getAllTask();
        for(Task t : taskList) {
            try{
                Class clazz = null;
                try {
                    clazz = Class.forName(t.getBeanClass());
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                if(clazz==null){
                    DebugUtils.savaDebugInfo(null,"Quartz Job 自启动记录","沒有这个Job类："+t.getBeanClass()+" #已经自动排除");
                    continue;
                }
                String beanClass = t.getBeanClass().toString();
                Class cls = Class.forName(beanClass);
                QuartzManager.addJob(sched, t.getJobName(), JOB_GROUP_NAME, t.getJobName(), TRIGGER_GROUP_NAME, cls, t.getCronExpression(), t.getStartTime(), t.getEndTime());
            }catch(Exception e) {
                throw new RuntimeException("初始化任务失败");
            }
        }
        //开启所有任务
        QuartzManager.startJobs(sched);
    }

    private List<Task> getAllTask() {
        if(taskDao==null){

        }
        return taskDao.getAllTask();
    }

    @Override
    public void startJob(int jobId) throws Exception {
        DebugUtils.savaDebugInfo(null,"Quartz Scheduler HashCode ：",String.valueOf(sched.hashCode()));

        Task t = new Task();
        t.setJobId(jobId);
        Task task = getTask(t);
        String beanClass = task.getBeanClass().toString();
        Class cls = Class.forName(beanClass);
        QuartzManager.addJob(sched, cls, task);
        //修改状态
        task.setStatus(true);
        task.setIsValidate(true);
        updateTaskStatus(task);
    }

    @Override
    public void pauseJob(int jobId) throws Exception {
        DebugUtils.savaDebugInfo(null,"Quartz Scheduler HashCode ：",String.valueOf(sched.hashCode()));

        Task t = new Task();
        t.setJobId(jobId);
        Task task = getTask(t);
        QuartzManager.removeJob(sched, task.getJobName());

        //关闭之后,修改对应的状态
        task.setStatus(false);
        task.setIsValidate(false);
        updateTaskStatus(task);
    }

    @Override
    public void addTask(Task task) {
        Date date = new Date();
        task.setCreatTime(date);
        task.setStatus(false);
        task.setIsValidate(false);
        taskDao.insertTask(task);
    }

    @Override
    public Task getTask(Task task) {
        return taskDao.getJob(task);
    }

    @Override
    public Map<String, Object> getTaskList(Task task) {

        int pageIndex = task.getPageIndex() == 0 ? 1 : task.getPageIndex();
        String where = " 1=1 ";

        String proType = task.getProType();
        String proName = task.getProName();
        if (!(proType == null) && !(proType == "") && !(proName == null) && !(proName == "")) {
            if (proType.equals("jobName")) {
                where += " and jobName like '%" + proName + "%'";
            }
            if (proType.equals("beanClass")) {
                where += " and beanClass like '%" + proName + "%'";
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("where", where);
        map.put("pageIndex", pageIndex);
        List<Task> list = taskDao.getJobList(map);
        map.put("list", list);
        return map;
    }

    @Override
    public void updateTask(Task task) {
        taskDao.updateTask(task);
    }

    @Override
    public void deleteTask(int taskId) {
        taskDao.deleteTask(taskId);
    }

    @Override
    public void updateTaskStatus(Task task) {
        taskDao.updateTaskStatus(task);
    }*/


}